import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from '../routes';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../../constants';
import {Link} from 'react-router-dom';
import {lcStatus, currency, lcKind, lcOfficialDraftType} from '../../Enum';
import {conventEnumValueToString, formatDateTime} from '../../utils';
import {injectIntl} from '../intl';
export class TableHistory extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total, sortedColumnKey, sortedOrder} = this.props;
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table className="white-space-nowrap"
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE} >
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.dealerCode',
                        defaultMessage: '总代编号'
                    })}
                    dataIndex="dealerCode"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'dealerCode' && sortedOrder}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.dealerName',
                        defaultMessage: '总代名称'
                    })}
                    dataIndex="dealerName"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'dealerName' && sortedOrder}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.lcCode',
                        defaultMessage: '信用证号码'
                    })}
                    dataIndex="lcCode"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'lcCode' && sortedOrder}
                    render={(text, record) => <Link key="detail" to={routes.detailhistory.format(record.id)}>{text}</Link>}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.issueCountryChineseName',
                        defaultMessage: '开证国家'
                    })}
                    dataIndex="issueCountryChineseName"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'issueCountryChineseName' && sortedOrder}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.issueBank',
                        defaultMessage: '开证银行'
                    })}
                    dataIndex="issueBank">
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.issuePrice',
                        defaultMessage: '开证金额'
                    })}
                    dataIndex="issuePrice">
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.currency',
                        defaultMessage: '币种'
                    })}
                    dataIndex="currency"
                    render={text => conventEnumValueToString(currency, text)}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.lcKind',
                        defaultMessage: '信用证类型'
                    })}
                    dataIndex="lcKind"
                    render={text => conventEnumValueToString(lcKind, text)}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.lcOfficialDraftType',
                        defaultMessage: '正本草本'
                    })}
                    dataIndex="lcOfficialDraftType"
                    render={text => conventEnumValueToString(lcOfficialDraftType, text)}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.issueDate',
                        defaultMessage: '开证日期'
                    })}
                    dataIndex="issueDate"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'issueDate' && sortedOrder}
                    render={text => formatDateTime(text, DATE_FORMAT)}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.dateOfExpiry',
                        defaultMessage: '效期'
                    })}
                    dataIndex="dateOfExpiry"
                    render={text => formatDateTime(text, DATE_FORMAT)}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.placeOfExpiry',
                        defaultMessage: '效地'
                    })}
                    dataIndex="placeOfExpiry">
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.creatorName',
                        defaultMessage: '创建人姓名'
                    })}
                    dataIndex="creatorName">
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.createTime',
                        defaultMessage: '创建时间'
                    })}
                    dataIndex="createTime"
                    render={text => formatDateTime(text, DATE_FORMAT)}>
                    </Table.Column>
                    <Table.Column title={formatMessage({
                        id: 'tableHistory.text.lcStatus',
                        defaultMessage: '状态'
                    })}
                    dataIndex="lcStatus"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'lcStatus' && sortedOrder}
                    render={text => conventEnumValueToString(lcStatus, text)}>
                    </Table.Column>
                </Table>
            </div>
        );
    }
}

TableHistory.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    refreshListData: PropTypes.func,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableSearch,
} from '../actions';
const getData = selectorFactory(['page', 'domainData', 'listHistory', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'listHistory', 'isFetching']),
    total: state.getIn(['page', 'domainData', 'listHistory', 'total']),
    errorMessage: state.getIn(['page', 'domainData', 'listHistory', 'message']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySortHistory', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySortHistory', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySortHistory', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySortHistory', 'sortedOrder']),
    permissions: getPermissions(state),
});
const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TableHistory));

